using System.Collections.Generic;
using UnityEngine;

namespace Gameplay.PVE
{
    public class RpgGameObjectHelper
    {
        private enum AlignType
        {
            Forward = 1,
            Position = 2,
        }
        
        private static Dictionary<Transform,List<TransformAlign>> transformAligns = new Dictionary<Transform,List<TransformAlign>>();
        
        private struct TransformAlign
        {
            //public Transform targetTransform;
            public Transform selfTransform;
            public AlignType alignType;
            public Vector3 offset;
        }
        
        public static void Update()
        {
            foreach (var item in transformAligns)
            {
                var targetTransform = item.Key;
                var list = item.Value;
                if (targetTransform == null)
                {
                    list.Clear();
                }
                else
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        var ta = list[i];
                        var selfTransform = ta.selfTransform;
                        if (targetTransform == null || selfTransform == null)
                        {
                            list.RemoveAt(i);
                            i--;
                        }
                        else
                        {
                            if (ta.alignType == AlignType.Forward)
                            {
                                if (selfTransform.forward != targetTransform.forward)
                                {
                                    selfTransform.forward = targetTransform.forward;
                                }
                            }

                            if (ta.alignType == AlignType.Position)
                            {
                                var newPosition = targetTransform.position + ta.offset;
                                if (selfTransform.position != newPosition)
                                {
                                    selfTransform.position = newPosition;
                                }
                            }
                        }
                    }
                }
            }
        }

        private static void AddAlign(Transform targetTransform, Transform selfTransform,AlignType alignType,Vector3 offset)
        {
            if (!transformAligns.TryGetValue(targetTransform, out var list))
            {
                list = new List<TransformAlign>();
                transformAligns.Add(targetTransform,list);
            }
            list.Add(new TransformAlign
            {
                selfTransform = selfTransform,
                alignType = alignType,
                offset = offset,
            });
        }

        private static void RemoveAlign(Transform targetTransform, Transform selfTransform,AlignType alignType)
        {
            if (transformAligns.TryGetValue(targetTransform, out var list))
            {
                for (int i = 0; i < list.Count; i++)
                {
                    var ta = list[i];
                    if (ta.selfTransform == selfTransform && ta.alignType == alignType)
                    {
                        list.RemoveAt(i);
                        i--;
                    }
                }
            }
        }
        
        public static void SetForwardAlign(Transform targetTransform,Transform selfTransform)
        {
            AddAlign(targetTransform, selfTransform, AlignType.Forward,Vector3.zero);
        }

        public static void RemoveForwardAlign(Transform targetTransform, Transform selfTransform)
        {
            RemoveAlign(targetTransform, selfTransform, AlignType.Forward);
        }
        
        public static void SetPositionAlign(Transform targetTransform, Transform selfTransform,Vector3 offset)
        {
            AddAlign(targetTransform, selfTransform, AlignType.Position,offset);
        }

        public static void RemovePositionAlign(Transform targetTransform, Transform selfTransform)
        {
            RemoveAlign(targetTransform, selfTransform, AlignType.Position);
        }
    }
}